package com.shop.product.dao;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.mybatis.spring.support.SqlSessionDaoSupport;
import org.springframework.stereotype.Repository;

import com.shop.product.vo.ProductVO;

@Repository
public class ProductDAOImpl extends SqlSessionDaoSupport implements ProductDAO {


	/**
	 * 상품 총 개수
	 * @param src_type
	 * @param src_keyword
	 * @return
	 */
	@Override
	public int getProductCount(String src_type, String src_keyword) {
		Map<String, Object> paramMap = new HashMap<String, Object>();
		paramMap.put("src_type", src_type);
		paramMap.put("src_keyword", src_keyword);
		return (Integer)getSqlSession().selectOne("com.shop.product.totalCnt", paramMap);
	}

	/**
	 * 상품 리스트
	 * @param current_page
	 * @param src_type
	 * @param src_keyword
	 * @return
	 */
	@Override
	@SuppressWarnings("unchecked")
	public List<ProductVO.ProductItem> listProduct(int current_page,
													int list_row_no,
													String src_type,
													String src_keyword) {
		//
		int start_row_index = (current_page - 1) * list_row_no;
		int end_row_index = list_row_no;
		
		Map<String, Object> paramMap = new HashMap<String, Object>();
		paramMap.put("src_type", src_type);
		paramMap.put("src_keyword", src_keyword);
		paramMap.put("start_row_index", start_row_index);
		paramMap.put("end_row_index", end_row_index);
		
		return getSqlSession().selectList("com.shop.product.list", paramMap);
	}	
	
	/**
	 * 상품 리스트 메인 이미지 조회
	 */
	@Override
	public ProductVO.ProductImg getMasterImg(String product_seq) {
		Map<String, Object> paramMap = new HashMap<String, Object>();
		paramMap.put("product_seq", product_seq);
		
		return (ProductVO.ProductImg)getSqlSession().selectOne("com.shop.product.master_img", paramMap);
	}
	
	@Override
	@SuppressWarnings("unchecked")
	public List<ProductVO.ProductComment> listProductComment(int product_seq) {
		return getSqlSession().selectList("com.shop.proudct.commentList");
	}
	
	
	
	@Override
	public int addProduct(ProductVO.ProductItem productItem) {
		getSqlSession().insert("com.shop.product.master_add", productItem);
		return productItem.getSeq();
	}
	
	/**
	 * 상품 이미지 등록
	 * @param productImg
	 */ 
	public void addImg(ProductVO.ProductImg productImg) {
		getSqlSession().insert("com.shop.product.img_add", productImg);
	}
	
	/**
	 * 상품 옵션 등록
	 * @param productOptDetail
	 */
	public void addOpt(ProductVO.ProductOptDetail productOptDetail) {
		getSqlSession().insert("com.shop.product.opt_add", productOptDetail);
	}
	
	/**
	 * 상품 태그 등록 (태그 = 카테고리)
	 * @param productTag
	 */
	public void addTag(ProductVO.ProductTag productTag) {
		getSqlSession().insert("com.shop.product.tag.add", productTag);
	}
}
